package com.ququ.syb.business.account.service;

import com.ququ.syb.business.account.redis.RedisDao;
import com.ququ.syb.common.constant.RedisKeyConstant;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;

import java.util.Set;

@Service
public class BlackListService {
    @Autowired
    private RedisDao redisDao;

    public void addBlackList(String idcardno, String mobile){
        Jedis jedis = redisDao.getJedis();
        try {
            if (StringUtils.isNotBlank(idcardno))
                jedis.sadd(RedisKeyConstant.BLACKLIST_IDCARD,idcardno);
            if (StringUtils.isNotBlank(mobile))
                jedis.sadd(RedisKeyConstant.BLACKLIST_MOBILE,mobile);
        }finally {
            redisDao.close(jedis);
        }
    }


    public void removeBlackList(String idcardno, String mobile){
        Jedis jedis = redisDao.getJedis();
        try {
            if (StringUtils.isNotBlank(idcardno))
                jedis.srem(RedisKeyConstant.BLACKLIST_IDCARD,idcardno);
            if (StringUtils.isNotBlank(mobile))
                jedis.srem(RedisKeyConstant.BLACKLIST_MOBILE,mobile);
        }finally {
            redisDao.close(jedis);
        }
    }


    public boolean isBlackListMobile(String mobile){
        Jedis jedis= redisDao.getJedis();
        try {
            return jedis.sismember(RedisKeyConstant.BLACKLIST_MOBILE,mobile);
        }finally {
            redisDao.close(jedis);
        }
    }


    public boolean isBlackListIdcard(String idcardno){
        Jedis jedis= redisDao.getJedis();
        try {
            return jedis.sismember(RedisKeyConstant.BLACKLIST_IDCARD,idcardno);
        }finally {
            redisDao.close(jedis);
        }
    }

    public Set<String> listBlackListMobile(){
        Jedis jedis= redisDao.getJedis();
        try {
            return jedis.smembers(RedisKeyConstant.BLACKLIST_MOBILE);
        }finally {
            redisDao.close(jedis);
        }
    }


    public Set<String> listBlackListIdcard(){
        Jedis jedis= redisDao.getJedis();
        try {
            return jedis.smembers(RedisKeyConstant.BLACKLIST_IDCARD);
        }finally {
            redisDao.close(jedis);
        }
    }

}
